package com.ruoyi.aitcommon.vo;

import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.util.StrUtil;

public class DBConVO{
//	数据库驱动
	private static final String SQLSERVER_DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static final String ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
	private static final String MYSQL_DRIVER = "com.mysql.cj.jdbc.Driver";
	private static final String POSTGRESQL_DRIVER="org.postgresql.Driver";
	public String url;
	public String username;
	public String password;
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "DBConVO [url=" + url + ", username=" + username + ", password=" + password + "]";
	}
	
	public boolean isSQLSERVER() {
		if(url.contains("sqlserver"))
			return true;
		else
			return false;
	}
	
	public boolean isORACLE() {
		if(url.contains("oracle"))
			return true;
		else
			return false;
	}
	
	public boolean isMYSQL() {
		if(url.contains("mysql"))
			return true;
		else
			return false;
	}
	
	public boolean isPOSTGRESQL() {
		if(url.contains("postgresql"))
			return true;
		else
			return false;
	}
	
	public String getDriveClassName(String url) throws Exception {
		if(StrUtil.isBlank(url))
			throw new Exception("url为空!");
		
		if (this.isSQLSERVER()) {
			return SQLSERVER_DRIVER;
		} else if (this.isORACLE()) {
			return ORACLE_DRIVER;
		} else if (this.isMYSQL()) {
			return MYSQL_DRIVER;
		} else if (this.isPOSTGRESQL()) {
			return POSTGRESQL_DRIVER;
		}
		else {
			String msg="[%s]不包含驱动信息!";
			msg=String.format(msg, url);
			throw new Exception(msg);
		}
	}
	
	/**
	 * 构造Druid配置信息
	 * 这里修改最大连接数等信息
	 * @return
	 */
	public Map<String,String> toDruidMap() throws Exception{
		Map<String,String> retmap=new HashMap<String,String>();
		retmap.put("url", url);
		retmap.put("username", username);
		retmap.put("password", password);
		retmap.put("driverClassName", this.getDriveClassName(url));
		return retmap;
	}
}